import type { TextDoc, Row } from './types';
import fs from 'fs';
import readline from 'readline';
import * as prettier from 'prettier';
import parser from './parser';

async function readFile(path: fs.PathLike): Promise<TextDoc> {
  const fileStream = fs.createReadStream(path);
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  const rows: Row[] = [];
  for await (const line of rl) {
    rows.push({ value: line, type: 'row' });
  }
  return { rows };
}

async function main() {
  const document = await readFile('./README.md');
  const pd = parser.parse(document);
  console.log(await prettier.format(JSON.stringify(pd), { parser: 'json' }));
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
